angular.module('mamApp').controller('materialController', ['$scope', 'CPExamineManager', '$state', '$uibModal', function ($scope, CPExamineManager, $state, $uibModal) {
    var cpId = $state.params.cpId;
    $scope.cpProperty = $state.params.cpProperty;

    $scope.formData = {};
    $scope.error_a = false;
    $scope.error_b = false;
    $scope.reviewErrorIds = [
        {key: 201, value: $scope.table[201]},
        {key: 202, value: $scope.table[202]},
        {key: 203, value: $scope.table[203]},
        {key: 204, value: $scope.table[204]}
    ];
    $scope.cpleveles = [
        {key: 20, value: $scope.table[20]},
        {key: 21, value: $scope.table[21]},
        {key: 22, value: $scope.table[22]},
    ];


    $scope.coopChange = function () {
        if ($scope.formData.coopType == 31) {
            $scope.formData.contractId = ''
        }
    }

    //model 点击打开。param：title文字、body数据、选择触发回调方法，size：model大小（big、small，默认中等大小）
    $scope.addLabel = function (param, elm, size) {
        var modalInstance = $uibModal.open({
            animation: true,
            templateUrl: 'addLabel.html',
            controller: 'addLabelController',
            resolve: {
                param: function () {
                    return {labelChecked:$scope.labelChecked}
                }
            }
        }).result.then(function (result) {
            $scope.labelChecked=result
        });
    };



    if ($scope.cpProperty == 12) {
        CPExamineManager.personalMaterial(cpId).then(function (result) {
            $scope.data = result.data.personalVO;
            $scope.labelChecked = result.data.personalVO.anchorTagName?result.data.personalVO.anchorTagName:[];

            $scope.formData.reviewErrorMemo = $scope.data.reviewErrorMemo;
            $scope.formData.coopType = $scope.data.coopType;
            $scope.formData.cpProperty = $scope.data.cpProperty;
            $scope.formData.reviewErrorId = $scope.data.reviewErrorId ? $scope.data.reviewErrorId : 201;
            $scope.formData.contractId = $scope.data.contractId;
            $scope.formData.cplevel = $scope.data.cplevel ? $scope.data.cplevel : 21;
            $scope.formData.cpId = cpId;
            $scope.contractFileUrl = $scope.data.contractFileUrl;
            $scope.submit = function () {
                if ($scope.data.reviewStatus != 7) {
                    if(($scope.formData.contractId!=0&&$scope.formData.coopType==30)||$scope.formData.coopType==31){
                        var reviewStatus = $scope.data.reviewStatus;
                        var anchorIds = [];
                        $scope.formData.reviewStatus = parseInt(reviewStatus);
                        $scope.formData.cpId = parseInt(cpId);
                        $scope.formData.reviewStatus = $scope.data.reviewStatus;
                        $scope.labelChecked.forEach(function (labelData) {
                            anchorIds.push(labelData.anchorId);
                        });
                        $scope.formData.anchorTag = anchorIds.join(",");

                        CPExamineManager.auditing(cpId, $scope.formData).then(function (result) {
                            if (result.data.c == "s") {
                                $state.go('main.CPExamine', {}, {reload: true})
                            } else {
                                alert("出现未明错误")
                            }
                        })
                    } else {
                        $scope.error_b = true;
                    }
                }else {
                    $scope.error_a= true;
                }
            }

        })
    } else {
        CPExamineManager.companyMaterial(cpId).then(function (result) {
            $scope.data = result.data.companyVO;
            $scope.labelChecked = result.data.companyVO.anchorTagName?result.data.companyVO.anchorTagName:[];

            $scope.formData.reviewErrorMemo = $scope.data.reviewErrorMemo;
            $scope.formData.coopType = $scope.data.coopType;
            $scope.formData.cpProperty = $scope.data.cpProperty;
            $scope.formData.contractId = $scope.data.contractId;
            $scope.formData.cplevel = $scope.data.cplevel ? $scope.data.cplevel : 21;
            $scope.formData.reviewErrorId = $scope.data.reviewErrorId ? $scope.data.reviewErrorId : 201;
            $scope.formData.cpId = cpId;
            $scope.contractFileUrl = $scope.data.contractFileUrl;
            $scope.submit = function () {
                if ($scope.data.reviewStatus != 7) {
                    if(($scope.formData.contractId!=0&&$scope.formData.coopType==30)||$scope.formData.coopType==31){
                        var reviewStatus = $scope.data.reviewStatus;
                        var anchorIds = [];
                        $scope.formData.reviewStatus = parseInt(reviewStatus);
                        $scope.formData.cpId = parseInt(cpId);
                        $scope.formData.reviewStatus = $scope.data.reviewStatus;
                        $scope.labelChecked.forEach(function (labelData) {
                            anchorIds.push(labelData.anchorId);
                        });
                        $scope.formData.anchorTag = anchorIds.join(",");

                        CPExamineManager.auditing(cpId, $scope.formData).then(function (result) {
                            if (result.data.c == "s") {
                                $state.go('main.CPExamine', {}, {reload: true})
                            } else {
                                alert("出现未明错误")

                            }
                        })
                    }else {
                        $scope.error_b = true;
                    }

                }
                else {
                    $scope.error_a = true;
                }
            }

        })
    }


    $scope.statusChange = function () {
        if ($scope.data.reviewStatus == 8) {
            $scope.error_a = false;
            $scope.formData.cplevel = $scope.data.cplevel ? $scope.data.cplevel : 21;
            $scope.formData.reviewErrorId="";

        } else if ($scope.data.reviewStatus == 9)  {
            $scope.error_a = false;
            $scope.formData.reviewErrorId = $scope.data.reviewErrorId ? $scope.data.reviewErrorId : 201;

        }
    }



    $scope.contractID = false;
    $scope.chooseContract = function () {
        var modalInstance = $uibModal.open({
            animation: true,
            templateUrl: 'chooseContract.html',
            controller: 'chooseContractController'
        })
        modalInstance.result.then(function (result) {
            $scope.contractData = result;
            $scope.contractID = true;
            $scope.formData.contractId = $scope.contractData.id;
            $scope.contractFileUrl = $scope.contractData.fileUrl;
            $scope.error_b = false;
        })
    }


    $scope.agreement=false;
    $scope.cancel = function () {
        $state.go('main.CPExamine', {}, {reload: false})
    }
    $scope.materialAgreement = function(){
        $scope.agreement=true
    }
    $scope.history = function(){
        $scope.agreement=false
    }


}])

